# -*- coding:utf8 -*- #
# -----------------------------------------------------------------------------------
# ProjectName:  smart-meter-iot-test
# FileName:     select_updateCollector_sql.py
# Author  :
# Datetime:     2022/8/11 10:06
# Description：电表数据随机构建
# -----------------------------------------------------------------------------------
import random

# 有功功率数据 ，2位整数，4位小数，我们的电表最大通过电流100A，保护设置80A
# 最大功率我也搞不太清除怎么算，厂家说的电压乘以电流
from common.util.timeUtil.timeUtil import get_now_time


class MeterData:
    # 保留两位小数,不四舍五入
    # @staticmethod
    # def decimal_places(number) -> float:
    #     number = str(number)
    #     if "." not in number:
    #         return float(number)
    #     else:
    #         return float(number.split('.')[0] + '.' + number.split('.')[1])

    # 组合有功
    @staticmethod
    def zhyg_increment(bei: int):
        nowHours = int(get_now_time(2))
        # 夜晚时间
        nightTime_1 = random.randint(19, 23)
        nightTime_2 = random.randint(8, 10)
        # 不上报
        doNoReport = random.randint(1, 10)
        zhygIncrement = 0
        if doNoReport > 3:
            if bei == 1:
                if nowHours >= nightTime_1 or nowHours <= nightTime_2:
                    zhygIncrement = round(random.uniform(0.1, 1), 2)
                else:
                    zhygIncrement = round(random.uniform(0.1, 2.5), 2)
            else:
                if nowHours >= nightTime_1 or nowHours <= nightTime_2:
                    zhygIncrement = round(random.uniform(0.1, 0.6), 2)
                else:
                    zhygIncrement = round(random.uniform(0.1, 1.5), 2)
        else:
            zhygIncrement = zhygIncrement
        return round(zhygIncrement, 2)

    # 电流
    @staticmethod
    def dl(phase, zhygIncrement, dy) -> list:
        if phase == 1:
            dl = (zhygIncrement / dy)*100
            return dl.__round__(2)
        else:
            dl_a = (zhygIncrement / dy[0] / 3) * 100
            dl_b = (zhygIncrement / dy[1] / 3) * 100
            dl_c = (zhygIncrement / dy[2] / 3) * 100
            dl_list = [dl_a.__round__(2), dl_b.__round__(2), dl_c.__round__(2)]
            return dl_list

    # 电压
    @staticmethod
    def dy(phase) -> list:
        if phase == 1:
            dy = random.uniform(210, 235)
            return dy.__round__(2)
        else:
            dy_a = random.uniform(210, 235)
            dy_b = (dy_a + dy_a * random.uniform(0, 0.05))
            dy_c = (dy_a - dy_a * random.uniform(0, 0.05))
            dy_list = [dy_a.__round__(2), dy_b.__round__(2), dy_c.__round__(2)]
        return dy_list

    # 频率
    @staticmethod
    def frequency() -> float:
        frequency = random.uniform(47.5, 52.5).__round__(2)
        return frequency

    # 有功功率
    @staticmethod
    def power_yg(phase, dy, dl):
        if phase == 1:
            yg_sum = (dl * dy).__round__(2)
            return yg_sum
        else:
            yg_a = (dl[0] * dy[0])
            yg_b = (dl[1] * dy[1])
            yg_c = (dl[2] * dy[2])
            yg_sum = yg_a + yg_b + yg_c
            yg_list = [yg_sum.__round__(2), yg_a.__round__(2), yg_b.__round__(2), yg_c.__round__(2)]
        return yg_list

    # 无功功率
    @staticmethod
    def power_reactive():
        return 0

    # 功率因数
    @staticmethod
    def power_factor() -> list:
        return 1

    # 信号
    @staticmethod
    def signal() -> int:
        return random.randint(18, 32)

    # 电表分合闸状态
    @staticmethod
    def status() -> str:
        # status = ["off", "on"]
        # return random.choice(status)
        return "on"


if __name__ == '__main__':
    md = MeterData()
    phase = 3
    dy = [220, 221, 223]
    # print(md.power_yg(phase, md.dl(phase, md.zhyg_increment(1), md.dy(phase)), md.dy(phase)))
    # print(md.dl(phase, 2.2, dy))
    print(md.dl(3,0.33,dy))
